package com.zhugang.week01;

/**
 * @program algorithms
 * @description: oneEdit
 * @author: chanzhugang
 * @create: 2022/04/07 07:05
 */
public class OneEdit2 {

    public static void main(String[] args) {
        OneEdit2 edit2 = new OneEdit2();
        boolean flag = edit2.oneEdit2("abc", "abde");
        System.out.println(flag);
    }

    public boolean oneEdit2(String first, String second) {
        int m = first.length();
        int n = second.length();
        // 长度差大于1
        if (Math.abs(m - n) > 1) {
            return false;
        }

        int diffCount = 0;
        // 长度差相等
        if (m == n) {
            for (int i = 0; i < m; i++) {
                if (first.charAt(i) != second.charAt(i)) {
                    diffCount++;
                }
            }
            return diffCount <= 1;
        }

        diffCount = 0;
        // 长度相差1，插入或者删除
        int i = 0, j = 0;
        while (i < m && j < n) {
            if (first.charAt(i) == second.charAt(j)) {
                i++;
                j++;
            } else {
                diffCount++;
                if (m > n) {
                    i++;
                } else {
                    j++;
                }
            }
        }
        return diffCount <= 1;
    }
}